<?php

//统计用户每日充值
$m = new Mongoclient("mongodb://127.0.0.1:27017");
$db = 'hs';
$db = $m->selectDB($db);
$table_tongji = 'tongji_user_recharge';
$collection_tongji = new MongoCollection($db, $table_tongji);

//2.定义sql语句
$connect=mysqli_connect('localhost','root','wlxiw#983kckwz8lai','heshui','3306');
mysqli_query($connect,'set names utf8');

//查询学校
$sql_school = 'SELECT * FROM hs_school';
$school_res = mysqli_query($connect,$sql_school);
$school_list = mysqli_fetch_all($school_res, MYSQLI_ASSOC);

// for ( $i=1;$i<33;$i++ ){
	
	$now_time=time()-24*3600;
	$today_time=date('Y-m-d H:i:s',$now_time);
	$today_date=date('Y-m-d',$now_time);
	
	$count_data = get_tongji_recharge($today_date,$connect,$school_list);
	
	if ( !empty($count_data) ){
		save_tongji_recharge($count_data, $today_date ,$collection_tongji);
	}
	
// }

//统计每日用户充值
function get_tongji_recharge($today_date,$connect,$school_list){
	
	$begin_time=strtotime($today_date.' 00:00:00');
	$end_time=strtotime($today_date.' 23:59:59');
	
	$sql = 'SELECT id,userID,amount FROM hs_recharge_order WHERE tradeState="SUCCESS" AND payTime >='.$begin_time.' AND payTime<='.$end_time;
	
	$result = mysqli_query($connect,$sql);
	
	
	$recharge_list = array();
	
	if ( $result ){
		//取出第一行判断数组是否为空
		$row = mysqli_fetch_row($result);
		if ( !empty($row) ){
			foreach ($result as $v){
				$recharge_list[$v['userID']]['uid'] = (int)$v['userID'];
				$recharge_list[$v['userID']]['amount_count'] = (int)(isset($recharge_list[$v["userID"]]['amount_count'])?($recharge_list[$v["userID"]]['amount_count']+$v['amount']):$v['amount']);
			}
		}
		
		if ( !empty($recharge_list) ){
			
			$userid_list = array_keys($recharge_list);
			$userid_list = array_unique($userid_list);
			$userid_list = implode(',', $userid_list);
			//查询用户
			$sql_user = 'SELECT id,nickname,schoolID FROM hs_user WHERE id IN ('.$userid_list.')';
			
			$userRes = mysqli_query($connect,$sql_user);
			$userList = mysqli_fetch_all($userRes, MYSQLI_ASSOC);
			
			foreach ( $recharge_list as $n=>$m){
				foreach ($userList as $nn=>$mm){
					if ($m['uid'] == $mm['id']){
						$recharge_list[$n]['name'] = $mm['nickname'];
						$recharge_list[$n]['sname'] = chose_schoole($mm['schoolID'],$school_list);
						$recharge_list[$n]['sid'] = (int)$mm['schoolID'];
						break;
					}else{
						$recharge_list[$n]['name'] = '';
						$recharge_list[$n]['sname'] = '';
						$recharge_list[$n]['sid'] = '';
					}
				}
			}
			
			
		}
		
	}
	return $recharge_list;
	
}

function chose_schoole($sid,$school_list){
	foreach ( $school_list as $school){
		if ( $school['id'] == $sid ){
			return $school['name'];
		}
	}
}

function save_tongji_recharge($userdata=array(),$now_time,$collection){

	foreach($userdata as $k=>$caiji_data){
		
		$select_data=$collection->findOne(array('uid'=>$caiji_data['uid'],'ctime'=>$now_time));
		
		if(!$select_data){//不存在则插入
			
			$caiji_data['ctime']=$now_time;

			$is_insert=$collection->insert($caiji_data);
			
			if($is_insert){
				echo 'i'.'--'.$caiji_data['uid']."_";
			}
			
		}else{
			unset($caiji_data['uid']);
			$thismongoid=$select_data['_id'];
			$update_data=array('$set'=>$caiji_data);
			$isuse_update=$collection->update(array('_id'=>new MongoId("$thismongoid")),$update_data,array('upsert'=>true));
			echo 'u'."_";
		}
	}

}
